package com.haieros.www.purerunshowsystem.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.haieros.www.purerunshowsystem.MusicModel;
import com.haieros.www.purerunshowsystem.VideoModel;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by admin on 2017/11/20.
 */

public class SDUtils {
    private String file; // 设置文件存放路径
    private String fileName; // 存放文件名称
    private Context context; // 获取到Context 上下文
    private int rawid; // 资源文件ID ，需要COPY 的文件
    private String DATABASE_PATH = "";
    private String DATABASE_NAME = "";

    public String getFile() {
        return file;
    }

    public String getFileUri(){
        return DATABASE_PATH + "/" + DATABASE_NAME;
    }

    public void setFile(String file) {
        this.file = file;
        this.DATABASE_PATH = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + file;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
        this.DATABASE_NAME = fileName;
    }

    public int getRawid() {
        return rawid;
    }

    public void setRawid(int rawid) {
        this.rawid = rawid;
    }

    public SDUtils() {
    }

    /**
     *
     * @param file
     *            文件夹例如： aa/bb
     * @param fileName
     *            文件名
     * @param context
     *            上下文
     * @param rawid
     *            资源ID
     */
    public SDUtils(String file, String fileName, Context context, int rawid) {
        super();
        this.file = file;
        this.fileName = fileName;
        this.context = context;
        this.rawid = rawid;
        this.DATABASE_PATH = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + file;
        this.DATABASE_NAME = fileName;
    }

    /**
     * 写入文件 返回路径
     */
    public String writeFile(String file, String fileName, Context context, int rawid) throws IOException {
        this.file = file;
        this.fileName = fileName;
        this.context = context;
        this.rawid = rawid;
        this.DATABASE_PATH = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + file;
        this.DATABASE_NAME = fileName;
        getSQLiteDatabase();
        return DATABASE_PATH + "/" + DATABASE_NAME;
    }

    /**
     * 将文件复制到SD卡，并返回该文件对应的数据库对象
     *
     * @return
     * @throws IOException
     */
    public String getSQLiteDatabase() throws IOException {

        // 首先判断该目录下的文件夹是否存在
        File dir = new File(DATABASE_PATH);
        String filename1 = DATABASE_PATH + "/" + DATABASE_NAME;
        if (!dir.exists()) {
            // 文件夹不存在 ， 则创建文件夹
            dir.mkdirs();
        }

        // 判断目标文件是否存在
        File file1 = new File(dir, DATABASE_NAME);

        if (!file1.exists()) {
            Log.i("msg", "没有文件，开始创建");
            file1.createNewFile(); // 创建文件
        }

        Log.i("msg", "准备开始进行文件的复制");
        // 开始进行文件的复制
        InputStream input = context.getResources().openRawResource(rawid); // 获取资源文件raw
        // 标号
        try {

            FileOutputStream out = new FileOutputStream(file1); // 文件输出流、用于将文件写到SD卡中
            // -- 从内存出去
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = (input.read(buffer))) != -1) { // 读取文件，-- 进到内存
                out.write(buffer, 0, len); // 写入数据 ，-- 从内存出
            }

            input.close();
            out.close(); // 关闭流

            return file1.getAbsolutePath();
        } catch (Exception e) {

            e.printStackTrace();
        }

        return null;

    }

    public String getFilePath(){
        return DATABASE_PATH;
    }

    /**
     * 获取指定目录下所有视频  默认为根目录
     * @param list
     * @param file
     * @return 视频信息列表
     */
    public static List<VideoModel> getVideoFile(final List<VideoModel> list, File file) {

        if (file == null){
            file = Environment.getExternalStorageDirectory();

        }

        file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {

                String name = file.getName();

                int i = name.indexOf('.');
                if (i != -1) {
                    name = name.substring(i);
                    if (name.equalsIgnoreCase(".mp4")
                            || name.equalsIgnoreCase(".3gp")
                            || name.equalsIgnoreCase(".wmv")
                            || name.equalsIgnoreCase(".ts")
                            || name.equalsIgnoreCase(".rmvb")
                            || name.equalsIgnoreCase(".mov")
                            || name.equalsIgnoreCase(".m4v")
                            || name.equalsIgnoreCase(".avi")
                            || name.equalsIgnoreCase(".m3u8")
                            || name.equalsIgnoreCase(".3gpp")
                            || name.equalsIgnoreCase(".3gpp2")
                            || name.equalsIgnoreCase(".mkv")
                            || name.equalsIgnoreCase(".flv")
                            || name.equalsIgnoreCase(".divx")
                            || name.equalsIgnoreCase(".f4v")
                            || name.equalsIgnoreCase(".rm")
                            || name.equalsIgnoreCase(".asf")
                            || name.equalsIgnoreCase(".ram")
                            || name.equalsIgnoreCase(".mpg")
                            || name.equalsIgnoreCase(".v8")
                            || name.equalsIgnoreCase(".swf")
                            || name.equalsIgnoreCase(".m2v")
                            || name.equalsIgnoreCase(".asx")
                            || name.equalsIgnoreCase(".ra")
                            || name.equalsIgnoreCase(".ndivx")
                            || name.equalsIgnoreCase(".xvid")) {
                        VideoModel video = new VideoModel();
                        file.getUsableSpace();
                        video.setName(file.getName());
                        video.setPath(file.getAbsolutePath());
                        Log.e("tga","name"+video.getPath());
                        list.add(video);
                        return true;
                    }
                    //判断是不是目录
                } else if (file.isDirectory()) {
                    getVideoFile(list, file);
                }
                return false;
            }
        });

        return list;
    }
    /**
     * 获取指定目录下所有音乐  默认为根目录
     * @param list
     * @param file
     * @return 视频信息列表
     */
    public static List<MusicModel> getMusicFile(final List<MusicModel> list, File file) {

        if (file == null){
            file = Environment.getExternalStorageDirectory();

        }

        file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {

                String name = file.getName();

                int i = name.indexOf('.');
                if (i != -1) {
                    name = name.substring(i);
                    if (name.equalsIgnoreCase(".mp3")
                            || name.equalsIgnoreCase(".m4a")
                            || name.equalsIgnoreCase(".wmv")
                            || name.equalsIgnoreCase(".wav")
                     )
                    {
                        MusicModel video = new MusicModel();
                        file.getUsableSpace();
                        video.setName(file.getName());
                        video.setPath(file.getAbsolutePath());
                        Log.e("tga","name"+video.getPath());
                        list.add(video);
                        return true;
                    }
                    //判断是不是目录
                } else if (file.isDirectory()) {
                    getMusicFile(list, file);
                }
                return false;
            }
        });

        return list;
    }
}
